<template>
  <v-dialog :title="title" :close-on-click-modal="false" v-model="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''" :disabled="method==='view'" label-width="130px">
      <el-row >
        <!-- <el-col :span="12">
          <el-form-item label="合同" prop="contract.id" :rules="[
                 ]">
            <GridSelect title="选择合同id" labelName='contractName' labelValue='id' v-model="inputForm.contract.id" :limit="1" :columns="[
            {
              prop: 'contractName',
              label: '合同名称'
            },
            {
              prop: 'contractNo',
              label: '合同编号'
            }
            ]" :searchs="[
            {
              prop: 'contractName',
              label: '合同名称'
            },
            {
              prop: 'contractNo',
              label: '合同编号'
            }
            ]" :api="tPurchaseContractService">
            </GridSelect>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="标段名称" prop="section"  :rules="[{required: true, message:'标段名称不能为空', trigger:'blur'}
                 ]">
            <el-input v-model="inputForm.section" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
            <el-form-item label="采购方式" prop="tenderType"
                :rules="[{required: true, message:'采购方式不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.tenderType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('procurement_method')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预算金额(万元)" prop="budgetAmount"  :rules="[                 ]">
            <el-input v-model="inputForm.budgetAmount" />
          </el-form-item>
        </el-col>
 

        <el-col :span="12" v-if="inputForm.tenderType!=4">
          <el-form-item label="公告发布时间" prop="tenderNoticeTime" :rules="[
                 ]">
            <el-date-picker style="width: 100%;" v-model="inputForm.tenderNoticeTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>     <el-col :span="12" v-if="inputForm.tenderType!=4">
          <el-form-item label="开标时间" prop="openBidTime" :rules="[
                 ]">
            <el-date-picker style="width: 100%;" v-model="inputForm.openBidTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="中标时间" prop="winBidTime" :rules="[
                 ]">
            <el-date-picker style="width: 100%;" v-model="inputForm.winBidTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col> -->
  
        <el-col :span="12" v-if="inputForm.tenderType!=4">
          <el-form-item label="中标时间" prop="winBidTime" :rules="[
                 ]">
            <el-date-picker style="width: 100%;" v-model="inputForm.winBidTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="inputForm.tenderType!=4">
            <el-form-item label="中标情况" prop="status"
                :rules="[                 ]">
                <el-select v-model="inputForm.status" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('tender_status')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
      


          <el-form-item label="中标单位" prop="winBidCompany">
            <GridSelect title="选择招标单位" labelName="companyName" labelValue="id" v-model="inputForm.winBidCompany.id" :limit="1" :columns="[
                       
                              { prop: 'companyName', label: '单位名称' }
                            ]" :searchs="[
                              { prop: 'companyName', label: '单位名称' }
                            ]" :api="correspondentService">
            </GridSelect>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="中标金额(万元)" prop="winBidAmount">
            <el-input v-model="inputForm.winBidAmount" />
          </el-form-item>
        </el-col>
           
        <el-col :span="24" v-if="inputForm.tenderType!=4">
          <el-form-item label="招标公告" prop="tenderNotice" :rules="[
                 ]">
            <file-upload v-model="inputForm.tenderNotice" :drag='true'>
              <div class="upload-content">
                <el-icon class="el-icon--upload"><upload-filled /></el-icon>
                <div class="upload-text">将文件拖到此处，或<el-buttom>点击上传</el-buttom></div>
                <div class="upload-tip">支持格式：PDF、Word，单个文件不超过10MB</div>
              </div>
            </file-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="inputForm.tenderType!=4">
          <el-form-item label="中标通知书信息" prop="winBidNotice" :rules="[
                 ]">
            <file-upload v-model="inputForm.winBidNotice" :drag='true'>
              <div class="upload-content">
                <el-icon class="el-icon--upload"><upload-filled /></el-icon>
                <div class="upload-text">将文件拖到此处，或<el-buttom>点击上传</el-buttom></div>
                <div class="upload-tip">支持格式：PDF、Word，单个文件不超过10MB</div>
              </div>
            </file-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-tabs v-model="purchaseTenderTab">
            <el-tab-pane label="报名情况(报名单位名称)">
              <el-button @click="addPurchaseTenderFirmRow" type="primary" style="margin-bottom:20px">新增</el-button>
              <el-table class="table" size="small" :data="inputForm.purchaseTenderFirmDTOList.filter((item) => { return item.delFlag !== '1'})"
                style="width: 100%">
                <el-table-column prop="company.companyName" header-align="center" align="center" show-overflow-tooltip label="投标单位">
                  <template #default="{row}">
                    <GridSelect title="选择投标单位" labelName='companyName' labelValue='id' v-model="row.company.id" :limit="1" :columns="[
                         
                            {
                              prop: 'companyName',
                              label: '单位名称'
                            }
                            ]" :searchs="[
                          
                            {
                              prop: 'companyName',
                              label: '单位名称'
                            }
                            ]" :api="correspondentService">
                    </GridSelect>
                  </template>
                </el-table-column>
                <el-table-column prop="" header-align="center" align="center" show-overflow-tooltip label="联系人">
                  <template #default="{row}">
                    <el-input v-model="row.contactPerson" placeholder="请填写联系人"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="" header-align="center" align="center" show-overflow-tooltip label="联系电话">
                  <template #default="{row}">
                    <el-input v-model="row.contactPhone" placeholder="请填写联系电话"></el-input>
                  </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="100">
                  <template #default="{row}">
                    <el-button @click="delPurchaseTenderFirmRow(row)" type="text" size="small">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false" icon="circle-close">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" icon="circle-check" v-noMoreClick>确定</el-button>
      </span>
    </template>
  </v-dialog>
</template>

<script>
import GridSelect from '@/components/gridSelect'
import purchaseTenderService from '@/api/tender/purchaseTenderService'
import tPurchaseContractService from '@/api/contract/tPurchaseContractService'
import correspondentService from '@/api/correspondent/correspondentService'

export default {
  data() {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      purchaseTenderTab: '0',
      tPurchaseContractService: tPurchaseContractService,
      correspondentService: correspondentService,
      inputForm: {
        id: '',
        purchaseTenderFirmDTOList: [],
        contract: {
          id: ''
        },
        project: {
          id: ''
        },
        purchase: {
          id: ''
        },
        tenderType:'',
        tenderNoticeTime: '',
        tenderNotice: '',
        winBidTime: '',
        winBidNotice: '',
        winBidCompany:{id:""},
        winBidName:"",
        tenderName:'',
        tenantDTO:{id:""},
      }
    }
  },
  components: {
    GridSelect
  },
   watch: {
    'inputForm.tenderNotice': {
      handler(newVal) {
        if (newVal) {
          // 获取所有文件信息
          const files = newVal.split(',');
          const fileNames = [];
          const fileUrls = [];

          files.forEach(file => {
            if (file&&file.indexOf(',')) {
              const urlArr = file.split('&fileName=');
              if (urlArr.length >= 2) {
                const fileName = urlArr[urlArr.length - 1];
                const fileUrl = urlArr[0];
                fileNames.push(fileName);
                fileUrls.push(fileUrl);
              } else {
                return;
              }
            }
          });         
          if (fileNames.length) {
            this.inputForm.tenderName = fileNames.join(',');
            // 保存纯文件地址（不带文件名）
            // this.inputForm.attachment = fileUrls.join(',');
          }
        }
      },
      immediate: true
    },
      'inputForm.winBidNotice': {
      handler(newVal) {
        if (newVal) {
          // 获取所有文件信息
          const files = newVal.split(',');
          const fileNames = [];
          const fileUrls = [];

          files.forEach(file => {
            if (file&&file.indexOf(',')) {
              const urlArr = file.split('&fileName=');
              if (urlArr.length >= 2) {
                const fileName = urlArr[urlArr.length - 1];
                const fileUrl = urlArr[0];
                fileNames.push(fileName);
                fileUrls.push(fileUrl);
              } else {
                return;
              }
            }
          });         
          if (fileNames.length) {
            this.inputForm.winBidName = fileNames.join(',');
            // 保存纯文件地址（不带文件名）
            // this.inputForm.attachment = fileUrls.join(',');
          }
        }
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    init(method, id) {
      this.method = method
      if (method === 'add') {
        this.title = `新建采购招标`
          this.inputForm.id = ''
        this.inputForm.contract.id = id.contract.id;
        this.inputForm.purchase.id = id.purchase.id;
        this.inputForm.project.id = id.project.id;
          this.inputForm.tenderType = id.tenderType;
      } else if (method === 'edit') {
        this.inputForm.id = id
        this.title = '修改采购招标'
      } else if (method === 'view') {
        this.inputForm.id = id
        this.title = '查看采购招标'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        // this.inputForm.id = id
        this.purchaseTenderTab = '0'
        this.inputForm.purchaseTenderFirmDTOList = []
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          purchaseTenderService.queryById(this.inputForm.id).then((data) => {
            this.inputForm = data
            if(!this.inputForm.winBidCompany){
            this.inputForm.winBidCompany={id:""}
            }
            this.loading = false
          })
        }
      })
    },
    savePurchaseTenderFirmRow(child) {
      if (child[0] === '') {
        this.inputForm.purchaseTenderFirmDTOList.push(child[1])
      } else {
        this.inputForm.purchaseTenderFirmDTOList.forEach((item, index) => {
          if (item === child[0]) {
            this.inputForm.purchaseTenderFirmDTOList.splice(index, 1, child[1])
          }
        })
      }
    },
    addPurchaseTenderFirmRow() {
      this.inputForm.purchaseTenderFirmDTOList.push({
        company: {
          id: ''
        },
        contactPerson: '',
        contactPhone: ''
      })
    },
    delPurchaseTenderFirmRow(child) {
      this.inputForm.purchaseTenderFirmDTOList.forEach((item, index) => {
        if (item === child && item.id === '') {
          this.inputForm.purchaseTenderFirmDTOList.splice(index, 1)
        } else if (item === child) {
          item.delFlag = '1'
          this.inputForm.purchaseTenderFirmDTOList.splice(index, 1, item)
        }
      })
    },
    // 表单提交
    doSubmit() {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          purchaseTenderService.save(this.inputForm).then((data) => {
            this.visible = false
            this.$message.success(data)
            this.$emit('refreshDataList')
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

  
